package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MenuItem;
import android.view.VelocityTracker;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.R;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.adapter.GoodsAdapter;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.Product;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.fragment.dealerFragment.FragmentGoodsManagement;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.fragment.dealerFragment.FragmentGoodsProcurement;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.fragment.producerFragment.FragmentAddProduct;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.fragment.producerFragment.FragmentProductManagement;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.fragment.producerFragment.FragmentProfile;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.serviceConnection.ProductServiceConnection;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.serviceImpl.ProductService;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.utils.ImageUtils;

public class ActivityDealer extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;
    private FragmentGoodsManagement fragmentGoodsManagement;
    private ProductServiceConnection productConnection;

    @Override
    protected void onStart() {
        super.onStart();
        SharedPreferences sharedPreferences = getSharedPreferences("ACCOUNT_REMEMBER", MODE_PRIVATE);
        Log.d("userId", sharedPreferences.getInt("userId", -1) + " ");

        productConnection = new ProductServiceConnection(service -> {
            fragmentGoodsManagement.onServiceConnected(service);
        });
        Intent intent = new Intent(this, ProductService.class);
        bindService(intent, productConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (productConnection.isBound()) {
            unbindService(productConnection);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dealer);

        bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(navListener);
        fragmentGoodsManagement = new FragmentGoodsManagement();

        // 默认选择第一个Fragment
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, fragmentGoodsManagement)
                    .commitNow();
        }
    }


    // 底部导航的监听事件
    private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment selectedFragment = null;

            int itemId = item.getItemId();
            if (itemId == R.id.nav_hot) {
                selectedFragment = new FragmentGoodsManagement();
            } else if (itemId == R.id.nav_library) {
                selectedFragment = new FragmentGoodsProcurement();
            } else if (itemId == R.id.per_info) {
                selectedFragment = new FragmentProfile();
            }

            switchToFragment(selectedFragment);

            return true;
        }
    };

    // 跳转Fragment
    private void switchToFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commitNow();
        if (fragment instanceof FragmentGoodsManagement) {
            ((FragmentGoodsManagement) fragment).onServiceConnected(productConnection.getService());
        } else if (fragment instanceof FragmentGoodsProcurement) {
            ((FragmentGoodsProcurement) fragment).onServiceConnected(productConnection.getService());
        }
    }
}



